$server = 'http://localhost:8080/';

window.onload = function(){
	$.ajax(
	{
		url: $server+'cart/list.do',
		type: "POST",
		datatype: 'json',		
		crossDomain:true, //设置跨域为true
		xhrFields:{
			withCredentials: true // 携带跨域cookie
		},
		success: function(result){
			console.log(result);
			if (result.code == 200){
				if (result.data == undefined || result.data.cartCommodityVoList == undefined || result.data.cartCommodityVoList.length == 0){
					$('.main')[0].innerHTML = '<p>购物车为空</p>';
					return;
				}
				
				// 是否全选
				if (result.data.allChecked == true){
					$('#allcheck2')[0].checked = true;
					$('#checkbox_all')[0].checked = true;
				}
				// 填充总价
				$('.price_top')[0].innerHTML = result.data.cartTotalPrice;
				$('.price_bottom')[0].innerHTML = result.data.cartTotalPrice;
				// 全部商品数
				$('#commoditySum')[0].innerHTML = result.data.commoditySum;
				
				$htmlstr = '';
				$.each(result.data.cartCommodityVoList, function(index, value){
					$isCheck = '';
					if (value.checked){
						$isCheck = 'checked="checked"';
					}
					
					$htmlstr += '<ul class="num2">' +
					    '<li class="check1">' +
					        '<input type="checkbox" name="yoga" '+$isCheck+' onclick=itemClick(this) data-commodity-id='+value.commodityId+' />' +
					        '<div class="img">' +
					            '<a href="javascript:void(0);" onclick=ckDetail(this) data-id>' +
					                '<img src="img/images/Yoga_03.jpg"/>' +
					            '</a>' +
					        '</div>' +
					    '</li>' +
					    '<li class="con_int">' +
					        '<div class="con_int_word">'+value.commodityName+'</div>' +
					    '</li>' +
					    '<li class="style">'+value.commoditySubtitle+'</li>' +
					    '<li class="unit_price1">￥' +
					        '<span>'+value.commodityPrice+'</span>' +
					    '</li>' +
					    '<li class="number">' +
					        '<div class="reduce" onclick=reduce(this) data-commodity-id='+value.commodityId+' >-</div>' +
					        '<input type="text" value="'+value.quantity+'"/>' +
					        '<div class="add" onclick=add(this) data-commodity-id='+value.commodityId+' >+</div>' +
					    '</li>' +
					    '<li class="subtotal1">￥' +
					        '<span>'+value.commodityTotalPrice+'</span>' +
					    '</li>' +
					    '<li>' +
					        // '<div class="in">移入收藏夹</div>' +
					        '<div class="remove" onclick=remove(this) data-commodity-id='+value.commodityId+'>删除</div>' +
					    '</li>' +
					'</ul>';
				});
				$('#carlist2').html($htmlstr);
			}else{
				alert(result.msg);
			}
		}
	});
}

// 商品选中或者不选中
function itemClick(obj){
	$commodityId = $(obj).attr('data-commodity-id');
	// 已经选中
	if (obj.checked){
		$.ajax(
		{
			url: $server+'cart/select.do',
			type: "POST",
			data: {commodityId: $commodityId},
			datatype: 'json',		
			crossDomain:true, //设置跨域为true
			xhrFields:{
				withCredentials: true // 携带跨域cookie
			},
			success: function(result){
				console.log(result);
			}
		});
	}else{
		$.ajax(
		{
			url: $server+'cart/un_select.do',
			type: "POST",
			data: {commodityId: $commodityId},
			datatype: 'json',		
			crossDomain:true, //设置跨域为true
			xhrFields:{
				withCredentials: true // 携带跨域cookie
			},
			success: function(result){
				console.log(result);
			}
		});
	}
	location.reload();  //实现页面重新加载
}

// 增加
function add(obj){
	$commodityId = $(obj).attr('data-commodity-id');
	var n = $(obj).prev().val();
	var num = parseInt(n) + 1;
	if (num <= 0) {
		return;
	}
	$.ajax(
	{
		url: $server+'cart/update.do',
		type: "POST",
		data: {commodityId: $commodityId, count: num},
		datatype: 'json',		
		crossDomain:true, //设置跨域为true
		xhrFields:{
			withCredentials: true // 携带跨域cookie
		},
		success: function(result){
			console.log(result);
		}
	});
	location.reload();  //实现页面重新加载
}

// 减少
function reduce(obj) {
	$commodityId = $(obj).attr('data-commodity-id');
	var n = $(obj).next().val();
	var num = parseInt(n) - 1;
	if (num <= 0) {
		return
	}
	$.ajax(
	{
		url: $server+'cart/update.do',
		type: "POST",
		data: {commodityId: $commodityId, count: num},
		datatype: 'json',		
		crossDomain:true, //设置跨域为true
		xhrFields:{
			withCredentials: true // 携带跨域cookie
		},
		success: function(result){
			console.log(result);
		}
	});
	location.reload();  //实现页面重新加载
}

// 删除
function remove(obj){
	$commodityId = $(obj).attr('data-commodity-id');
	$.ajax(
	{
		url: $server+'cart/remove.do',
		type: "POST",
		data: {commodityId: $commodityId},
		datatype: 'json',		
		crossDomain:true, //设置跨域为true
		xhrFields:{
			withCredentials: true // 携带跨域cookie
		},
		success: function(result){
			console.log(result);
		}
	});
	location.reload();  //实现页面重新加载
}